// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import FColliderManager from "./collider-system/FColliderManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    private nodeCount: number = 2000;
    @property(cc.Prefab)
    testPrefab: cc.Prefab = null;

    @property(cc.Node)
    parentNode: cc.Node = null;

    private pool: cc.NodePool = null;
    private width: number = cc.winSize.width;
    private height: number = cc.winSize.height;
    private halfWidth: number = this.width * 0.5;
    private halfHeight: number = this.height * 0.5;
    start() {
        this.height = cc.winSize.height;
        this.width = cc.winSize.width;
        this.halfWidth = this.width * 0.5;
        this.halfHeight = this.height * 0.5;
        console.log(cc.winSize.width, cc.winSize.height);
        FColliderManager.instance.enable = true;
        // FColliderManager.instance.enableQuadTreeDraw = true;
        // FColliderManager.instance.enableDebugDraw = true;

        this.pool = new cc.NodePool();
        for (let i = 0; i < this.nodeCount; i++) {
            this.createNode();
        }
    }
    private _list: cc.Node[] = [];
    createNode() {
        let node = this.pool.get();
        if (!node) node = cc.instantiate(this.testPrefab);
        node.parent = this.parentNode;
        node.x = (Math.random() - 0.5) * this.width;
        node.y = (Math.random() - 0.5) * this.height;
        node.color = cc.Color.WHITE;
        this._list.push(node);
        node["vx"] = Math.random() * 3 - 1.5;
        node["vy"] = Math.random() * 3 - 1.5;
    }

    protected update(dt: number): void {
        for (let i = 0; i < this._list.length; i++) {
            let node = this._list[i];
            node.x += node["vx"];
            node.y += node["vy"];
            if (node.x > this.halfWidth || node.x < -this.halfWidth) {
                node["vx"] *= -1;
            }
            if (node.y > this.halfHeight || node.y < -this.halfHeight) {
                node["vy"] *= -1;
            }
        }
    }

    private changeNormal() {
        cc.director.loadScene("helloworld2");
    }
}
